using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CrazyDogToDo.Entity.Data;

namespace CrazyDogToDo.Service.Services
{
    public class InventoryService
    {
        private CrazyDogToDoContext _crazy = new CrazyDogToDoContext();
        /// <summary>
        /// 浏览所有清单
        /// </summary>
        /// <returns></returns>
        public List<Inventory> GetAllInventory(string email)
        {
            var user = _crazy.Inventory.Where(a => a.Email.Equals(email) == true);
            if (user != null)
            {
                return user.ToList();
            }
            return null;
        }
        /// <summary>
        /// 清单的创建
        /// </summary>
        /// <param name="email"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool CreateInventory(string email, string title)
        {
            var listName = _crazy.Inventory.Where(u => u.Email.Equals(email)).Where(i => i.Title.Equals(title)).SingleOrDefault(t => t.CreateTime.Day == DateTime.Now.Day);/*找到用户下的重复名字的清单*/
            if (listName != null)/*如果用户没有找到 或者 清单名字已经存在*/
            {
                return false;
            }
            else
            {
                var list = new Inventory()
                {
                    Email = email,
                    Title = title,
                    CreateTime = DateTime.Now
                };
                _crazy.Inventory.Add(list);
                _crazy.SaveChanges();
                return true;

            }
        }
        /// <summary>
        /// 清单页面添加清单
        /// </summary>
        /// <param name="email"></param>
        /// <param name="title"></param>


        public void AddInventory(string email, string title)
        {
            var list = new Inventory()
            {
                Email = email,
                Title = title,
                CreateTime = DateTime.Now
            };
            _crazy.Inventory.Add(list);
            _crazy.SaveChanges();
        }
        /// <summary>
        /// 修改清单名字
        /// </summary>
        /// <param name="email"></param>
        /// <param name="newTitle"></param>
        /// <returns></returns>
        public bool ModifyInventoryName(string email, string newTitle)
        {
            var listName = _crazy.Inventory.Where(u => u.Email.Equals(email)).Where(i => i.Title.Equals(newTitle)).SingleOrDefault(t => t.CreateTime.Day == DateTime.Now.Day);/*找到用户下的重复名字的清单*/
            if (listName != null)/*如果用户没有找到 或者 清单名字已经存在*/
            {
                return false;
            }
            else
            {
                listName.Title = newTitle;
                _crazy.SaveChanges();
                return true;
            }
        }
        /// <summary>
        /// 删除清单
        /// </summary>
        /// <param name="email"></param>
        /// <param name="id"></param>
        public void DeleteInventory(int id)
        {
            var list = _crazy.Inventory.SingleOrDefault(n => n.Id == id);//找到要删除的清单对象
            var matter = _crazy.Matter.Where(n => n.InventoryId == id);  
            if (matter != null)
            {
                foreach (var item in matter)
                {
                    _crazy.Matter.Remove(item);
                }
            }
            _crazy.Inventory.Remove(list);
            _crazy.SaveChanges();
        }
        /// <summary>
        /// 查找清单
        /// </summary>
        /// <param name="createTime"></param>
        /// <returns></returns>
        public bool SearchInventory(string email, DateTime createTime)
        {
            var inventory = _crazy.Inventory.Where(u => u.Email.Equals(email))
                .Where(n => n.Title.Equals("我的一天"))
                .SingleOrDefault(I => I.CreateTime.Day == createTime.Day);
            if (inventory == null)
            {
                return false;
            }
            else
                return true;
        }

        public List<Matter> GetAllInventoryMatter(string email, int listId)
        {
            var list = _crazy.Inventory.Where(u => u.Email.Equals(email)).SingleOrDefault(n => n.Id == listId);
            if (list != null)
            {
                return list.Matter.Where(a => a.InventoryId == listId).ToList();
            }
            return null;
        }


        public List<Inventory> GetInventoryById(string email, int listId)
        {
            var list = _crazy.Inventory.Where(a => a.Email.Equals(email) == true).Where(h => h.Id == listId);
            if (list != null)
            {
                return list.ToList();
            }
            return null;
        }


    }

}
